<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="addType(1, '0')">
              添加权限
            </el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="perlist" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="addType(2, row.id)"
              >
                添加
              </el-button>
              <el-button type="text" @click="editPermission(row)">
                编辑
              </el-button>
              <el-button type="text" @click="delPermission(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹层 -->
    <el-dialog
      :visible="showDialog"
      :title="formData.id ? '编辑权限' : '添加权限'"
      @close="closeDialog"
    >
      <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="addPermission">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { getDepart } from '@/utils'
export default {
  data () {
    return {
      perlist: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.getperList()
  },
  methods: {
    async editPermission (row) {
      try {
        const res = await getPermissionDetail(row.id)
        console.log(res)
        this.showDialog = true
        this.formData = res
      } catch (error) {
        console.log(error)
      }
    },
    async delPermission (row) {
      console.log(row)
      if (row.children && row.children.length !== 0) {
        this.$message.error('该权限点下尚有权限 不能删除')
      } else {
        try {
          await this.$confirm(`确认删除${row.name}权限吗`, '提示')
          await delPermission(row.id)
          this.$message.success('删除成功')
          this.showDialog = false
          this.getperList()
        } catch (error) {
          console.log(error)
        }
      }
    },

    addType (type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async getperList () {
      const res = await getPermissionList()
      this.perlist = getDepart(res)
    },
    async addPermission () {
      await this.$refs.form.validate()
      if (!this.formData.id) {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      } else {
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
      }
      this.showDialog = false
      this.getperList()
    },
    async closeDialog () {
      try {
        await this.$refs.form.resetFields()
        this.formData = {
          enVisible: '0', // 开启
          name: '', // 名称
          code: '', // 权限标识
          description: '', // 描述
          type: '', // 类型
          pid: '' // 添加到哪个节点下
        }
      } catch (error) {
        console.log(error)
      }
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
